#!/bin/sh

#
# Test writing a sparse file resource to an offset
# This file is almost identical to 091_sparse_write2.test,
# but has the opposite order of hole/data.
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

SPARSE_FILE=$WORK/sparse.bin

# Sparse file support depends on the filesystem. The size
# of the sparse blocks may vary as well. To ensure that
# this test works across filesystems that support sparse
# files, keep everything on 4K boundaries.

if ! $FWUP_CREATE --sparse-check "$SPARSE_FILE" --sparse-check-size 4096; then
    echo "Skipping sparse file tests since OS or filesystem lacks support"
    exit 77
fi

if [ "$HOST_OS" = "Darwin" ]; then
    echo "Skipping test on APFS since hole creation varies between runs."
    exit 77
fi

if [ -n "$CIRCLECI" ]; then
    echo "Skipping test on CircleCI"
    exit 77
fi

TESTFILE_4K=$WORK/4k.bin
cat $TESTFILE_1K $TESTFILE_1K $TESTFILE_1K $TESTFILE_1K > $TESTFILE_4K

# This sparse file looks like this:
#
# Offset         Length        Contents
# 0              192K          Hole
# 192K           4K            $TESTFILE_4K
# 196K           192K          Hole
# 388K           4K            $TESTFILE_4K
# ...
#

i=0
while [ $i -lt 140 ]; do
    dd if=$TESTFILE_4K bs=1k seek=$(expr $i \* 196 + 192 || true) of=$SPARSE_FILE conv=sync,notrunc 2>/dev/null
    i=$(expr $i + 1)
done

cat >$CONFIG <<EOF
file-resource sparsefile {
        host-path = "${SPARSE_FILE}"
        skip-holes = true
}

task complete {
        on-resource sparsefile { raw_write(0) }
}
EOF

cat >$EXPECTED_META_CONF <<EOF
file-resource "sparsefile" {
length={0,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,4096,196608,2613248}
blake2b-256="aa73b4b09dd2518db12343f805f4bd1c7380b3b5f6b2e8060caf04609199ea3c"
}
task "complete" {
on-resource "sparsefile" {
funlist = {"2", "raw_write", "0"}
}
}
EOF

# Create the firmware file
$FWUP_CREATE -c -f $CONFIG -o $FWFILE
check_meta_conf

# Verify the file
$FWUP_VERIFY -V -i $FWFILE

# Apply it and check that the output compares the same as the input
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t complete
cmp_bytes 28098560 $SPARSE_FILE $IMGFILE
